package com.zero.flutter_gromore_ads.utils;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;

/**
 * InstrumentationProxy
 * 用于拦截快应用的跳转，屏蔽快应用的跳转
 */
public class InstrumentationProxy extends Instrumentation {
    private static final String TAG = "InstrumentationProxy";

    private Instrumentation mInstrumentation;

    public InstrumentationProxy(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
    }

    @Override
    public Activity newActivity(ClassLoader cl, String className, Intent intent) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        return mInstrumentation.newActivity(cl, className, intent);
    }

    private static boolean containsQuickAppScheme(String params) {
        if (TextUtils.isEmpty(params)) {
            return false;
        }
        return params.contains("hap://") ||
                params.contains("hnquickapp://") ||
                params.contains("hwfastapp://");
    }

    public static boolean isQuickApp(Context context, Intent intent) {
        if (intent == null) {
            return false;
        }
        Uri uri = intent.getData();
        if (uri == null) {
            return false;
        }
       
        //在这里判断scheme，处理自己的逻辑,这里可以对uri及scheme 进行打点收集，用于过滤统计及问题查看
        String scheme = intent.getScheme();

        //如果判定了是快应用的跳转，则直接return，屏蔽不跳转。
        if (!TextUtils.isEmpty(scheme) && (scheme.startsWith("hap") || scheme.startsWith("hnquickapp") || scheme.startsWith("hwfastapp"))) {
            return true;
        }
        //过滤使用intent://hapjs.org开头的intent
        Uri data = intent.getData();
        if (data != null) {
            String host = data.getHost();
            if(!TextUtils.isEmpty(scheme) && (scheme.startsWith("http") || scheme.startsWith("intent")) && !TextUtils.isEmpty(host) && host.startsWith("hapjs.org")){
                return true;
            }

            // 屏蔽利用hiapp或者higame 漏洞跳转的快应用
            String query = data.getQuery();
            if (!TextUtils.isEmpty(scheme) && (scheme.startsWith("hiapp") || scheme.startsWith("higame")) && !TextUtils.isEmpty(query)) {
                // Check for nested params
                if (query.contains("params=")) {
                    try {

                        String decode = URLDecoder.decode(query, "UTF-8");
                        if (containsQuickAppScheme(decode)) {
                            return true;
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        }

        return false;
    }
    

    
    public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,
                                            Intent intent, int requestCode, Bundle options) {
        try {
            ComponentName component = intent.getComponent();
            if (component != null) {
                Log.i(TAG, "execStartActivity() >>> componentName:" + component.getClassName() +
                        " packageName:" + component.getPackageName());
            }
            Log.i(TAG, "execStartActivity() >>> " + "who:" + who + " scheme:" + intent.getScheme());

            if (isQuickApp(who, intent)) {
                Log.i(TAG, "execStartActivity() >>> quick app");
                return null;
            }
            Method execStartActivityMethod = Instrumentation.class.getDeclaredMethod("execStartActivity",
                    Context.class, IBinder.class, IBinder.class, Activity.class,
                    Intent.class, int.class, Bundle.class);
            return (ActivityResult) execStartActivityMethod.invoke(mInstrumentation, who, contextThread, token, target,
                    intent, requestCode, options);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
}